package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.user.UserConstans;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.user.dto.ApUserRealnameDto;
import com.heima.model.user.dto.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.Feign.ApAuthorFeign;
import com.heima.user.Feign.WmUserFeign;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 按照状态查询用户认证列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadListByStatus(ApUserRealnameDto dto) {
        //判断参数是否为空
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //构建分页对象
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());

        //构建查询对象
        LambdaQueryWrapper<ApUserRealname> wrapper = Wrappers.lambdaQuery();
        if (null != dto.getStatus()) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus().intValue());
        }

        //查询用户信息
        IPage<ApUserRealname> iPage = page(page, wrapper);

        //构建响应对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    /**
     * 审核通过
     *
     * @param dto
     * @return
     */
    @Transactional
    @GlobalTransactional
    @Override
    public ResponseResult authPass(AuthDto dto) {

        //修改审核状态
        ResponseResult update = AURUpdateById(dto, UserConstans.PASS_AUTH);

        //判断是否修改成功
        if (update.getCode() != 200) {
            return update;
        }

        //创建自媒体账号， 以及作者账号并返回
        return createWmUserAndAuthor(dto);
    }

    /**
     * 审核驳回
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        return AURUpdateById(dto, UserConstans.FAIL_AUTH);
    }

    /**
     * 修改审核参数
     *
     * @param dto
     * @param status
     * @return
     */
    private ResponseResult AURUpdateById(AuthDto dto, short status) {
        //判断参数是否为空
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //创建修改对象
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        if (null != dto.getMsg()) {
            realname.setReason(dto.getMsg());
        }

        //修改审核状态
        realname.setStatus(status);

        //修改
        boolean update = updateById(realname);

        //判断是否修改成功
        if (update) {
            return ResponseResult.okResult(200, "修改成功");
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
    }

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private WmUserFeign wmUserFeign;

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    /**
     * 创建自媒体账号， 以及作者账号
     * @param dto
     * @return
     */
    private ResponseResult createWmUserAndAuthor(AuthDto dto) {
        //获取当前用户的userid
        ApUserRealname realname = getById(dto.getId());

        //获取当前用户的账号信息
        ApUser apUser = this.apUserMapper.selectById(realname.getUserId());

        //添加自媒体账户
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(apUser.getId());
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setImage(apUser.getImage());
        wmUser.setLoginTime(new Date());
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(UserConstans.WM_STATUS_AVAILABLE);
        wmUser.setType(0);
        ResponseResult seveWmUser = this.wmUserFeign.seveWmUser(wmUser);

        //判断是否添加成功
        if (seveWmUser.getCode() != 0) {
            return seveWmUser;
        }

        //添加作者账号
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId((Integer) seveWmUser.getData());
        apAuthor.setName(apUser.getName());
        apAuthor.setType(UserConstans.AUTH_TYPE_SELF);
        apAuthor.setCreatedTime(new Date());
        ResponseResult seveApAuthor = apAuthorFeign.seveApAuthor(apAuthor);

        //判断是否添加成功
        if (seveApAuthor.getCode() != 0) {
            return seveWmUser;
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
